<template>
  <div class="mainWarp">
    <div class="typeboxtit">
      <div class="tit">
        <p>商品分类</p>
      </div>
    </div>
    <van-tree-select v-model:active-id="activeId" @click-nav="navClick" v-model:main-active-index="activeIndex"
      height="86vh" :items="items">
      <template #content>
        <!-- <van-image v-if="activeIndex === 0" lazy-load
          src="http://www.tp5.com/uploads/20230430/20230430XP1E2xzsCOHGTbjrl5y6ZKY4tU3WugkLf45e2da28b6e6c546dbd1365c26cec47.jpg" />
        <van-image v-if="activeIndex === 1" lazy-load
          src="http://www.tp5.com/uploads/20230430/20230430hBQnerwOlWCJ0Zt2NF8jIP6v5muTpMfE2b0d076db84cbcdccdc601f6dd0f22c7.png" /> -->
        <div class="scrolltab-content">
          <div v-for="(item, index) in homeInfo.typeData" :key="item.id">
            <div class="scrolltab-content-item" v-if="activeIndex === index">
              <h2>{{ item.name }}</h2>
              <div>
                <div class="aui-class-ad-img"> <router-link :to="{path:'/product/product/index',query:{'typeid':item.id}}"> <img :src="item.avatar_cdn" alt=""> </router-link> </div>
                <div class="loading" v-if="!isLoading">
                  <van-loading type="spinner" vertical color="#1989fa">加载中...</van-loading>
                </div>
                <div class="aui-flex-links" v-else>
                  <div v-if="goods.length == 0">
                    暂无数据
                  </div>
                  <div v-else>
                    <router-link :to="{path:'/product/product/info',query:{'proid':item.id}}" class="aui-flex-links-item" v-for="item in goods" :key="item.id">
                      <div class="aui-flex-links-img"> <img :src="item.avatars_cdn[0]" alt=""> </div>
                      <div class="aui-flex-links-text">{{ item.name }}</div>
                    </router-link>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </van-tree-select>
  </div>
  <Tab />
</template>

<script setup>
import Tab from '@/components/common/Tab.vue'
import { ref, reactive, onMounted } from 'vue';
import apiList from '@/api'
import { useHomeStore } from '@/store/home.js'
import { storeToRefs } from 'pinia'

const homeStore = useHomeStore()
let { homeInfo } = storeToRefs(homeStore)

let activeIndex = ref(0);
const activeId = reactive([]);
const items = reactive([])
const goods = reactive([])
let isLoading = ref(false)

onMounted(async () => {
  homeInfo.value.typeData.map(({ id, name, ...rest }) => {
    activeId.push(id)
    items.push({ id, text: name, ...rest })
  })
  let res = await apiList.getTypelist({ typeid: activeId[0] })
  for (const item of res.data) {
    goods.push(item)
  }
  isLoading.value = true
})

async function navClick(index) {
  // goods.splice(0); // 将所有元素从零索引开始删除
  goods.length = 0//清空数组
  let res = await apiList.getTypelist({ typeid: activeId[index] })
  for (const item of res.data) {
    goods.push(item)
  }
  isLoading.value = true
}

</script>

<style lang="scss" scoped>
.tit {
  width: 100%;
  max-width: 512px;
  top: 0;
  height: 44px;
  z-index: 999;
  background: #fff;
  line-height: 44px;
}

p {
  padding-left: 12px;
  color: #444444;
  font-size: 16px;
  font-weight: bold;
}

.scrolltab-content {
  height: 100%;
  background-color: #f2f2f2;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  padding: 0.4rem 0.8rem 0.8rem 0.8rem;
  position: relative;
}

.scrolltab-content-item h2 {
  color: #4b4b4b;
  font-size: 14px;
  font-weight: normal;
  padding-bottom: 15px;
  padding-top: 10px;
}

.aui-flex-links {
  padding: 10px 8px;
  position: relative;
  overflow: hidden;
  background: #fff;
  border-radius: 5px;
  margin-top: 15px;
  margin-bottom: 15px;

  .aui-flex-links-item {
    position: relative;
    float: left;
    padding: 5px 0;
    width: 27.333333%;
    box-sizing: border-box;
    margin: 0 3%;

    .aui-flex-links-img {
      width: 100%;
      height: 100%;
      margin: 0 auto;

      img {
        width: 100%;
        height: auto;
        display: block;
        border: none;
      }
    }

    .aui-flex-links-text {
      color: #444;
      font-size: 12px;
      text-align: center;
      padding-top: 5px;
    }
  }
}

.--van-tree-select-content-background {
  background-color: #f2f2f2;
}
</style>
